package com.zhitian.data;



import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.csvreader.CsvWriter;




public class Pinghang {
	//获取当前文件夹下所有 含有表内容的html文件  
	//test:"D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content"
	public static List<String> getAllHtml(String folderName){
		List<String> htmlFileList = new ArrayList<String>();
		boolean isMatch;
		String pattern = "PingHangReportPage.*.html";
		File input = new File(folderName);
		for (String s : input.list()) {
			isMatch = Pattern.matches(pattern, s);
			if(isMatch) {
				htmlFileList.add(s);
			}
		}
		//确保list里的html文件是 按顺序排的
		Collections.sort(htmlFileList, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return getNumber(o1) -getNumber(o2);
			}
		});
		
		return htmlFileList;
	}
	//获取最后一个文件的后缀数字 例如PingHangReportPage_黄小雨862373030759292_246.html 中的246 方便后面按顺序爬取文件
	public static int getNumber(String file) {
		String regex = "(\\D*)(\\d+)(.*)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matchers = pattern.matcher(file);
		if (matchers.find( ) && !matchers.group(3).equals(".html") ) {
	         String numReg="[^0-9]";
	         Pattern p = Pattern.compile(numReg);
	         Matcher matcher2 = p.matcher(matchers.group(3));
	         return Integer.parseInt(matcher2.replaceAll(""));
	        
	     } 
		return 0;
		
	}
	
	
	//判断字符串是否全为数字
	public static boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
    }
	//正则表达式 是否匹配  数字-数字  为QQ群聊中的对方
	public static boolean isQQGroup(String str) {
		
		Pattern pattern = Pattern.compile("^(\\d+)(-)(\\d+).*");
		return pattern.matcher(str).matches();
	}
	//匹配任何以字母开头的字符串
	public static boolean  isWxId(String str) {
		Pattern pattern = Pattern.compile("^[A-Za-z].*");
		return pattern.matcher(str).matches();
	}
	
	//创建CsvWriter
	public static CsvWriter createCsvWriter(String path,String[] header) throws IOException {
		
		FileOutputStream fos = new FileOutputStream(path);
		//在文件中增加BOM，该处的byte[] 可以针对不同编码进行修改
		fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
		CsvWriter writer = new CsvWriter(fos,',',Charset.forName("UTF-8"));
		//String[] header = {"序号","时间","动作","对方","内容"};
		writer.writeRecord(header);
		
		return writer;
	}
	
	//关闭CsvWrite
	public static void  closeCsvWriter(List<CsvWriter> list) {
		for(CsvWriter csvWriter : list) {
			csvWriter.flush();
			csvWriter.close();
		}
		
	}
	//删除文件
	public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


	 
	/**
	 * 遍历html文件，爬取需要的数据，并存入csv
	 * @param folder   "D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content\\"
	 * @param csvPath   需要存放csv的路径
	 * @throws IOException
	 */
	public static void getDataInCsv(String folder,String csvPath,boolean get_applist,boolean get_contacts,
			boolean get_callsRecord,boolean get_message,boolean get_wechat,boolean get_QQ,
			boolean get_alipay,boolean get_systemInfo) throws IOException {
		List<String> htmls = getAllHtml(folder);
		
		System.out.println("-------getDataInCsv---------");
		System.out.println("被处理的文件夹："+folder);
		System.out.println("csv文件输出路径："+csvPath);
		
		//准备存储应用列表的csv
		String applist = csvPath+"应用列表.csv";
		CsvWriter applist_writer = createCsvWriter(applist, 
				new String[] {"序号","应用","事件数量","联系人数量","文件数量"
						,"删除数量","总量"});
		
		//通讯录csv
		String contacts = csvPath+"通讯录.csv";
		CsvWriter contacts_writer = createCsvWriter(contacts,
				new String[] {"序号","ID","昵称","姓名","手机","邮箱","归属地"});
		
		//通话记录
		String calls = csvPath+"通话记录.csv";
		CsvWriter calls_writer = createCsvWriter(calls, 
				new String[] {"序号","时间","动作","对方","号码","内容"});
		
		//短信
		String message = csvPath+"短信.csv";
		CsvWriter message_writer = createCsvWriter(message, 
				new String[] {"序号","时间","动作","对方","号码","内容"});
		
		//微信账号信息
		String wx_account_info = csvPath+"微信账号信息.csv";
		CsvWriter wx_account_info_writer = createCsvWriter(wx_account_info, 
				new String[] {"应用","账号","账号名","显示名","密码"
						,"邮箱","手机","性别","地址"});
		//QQ账号信息
		String QQ_account_info = csvPath+"QQ账号信息.csv";
		CsvWriter QQ_account_info_writer = createCsvWriter(QQ_account_info, 
				new String[] {"应用","账号","账号名","显示名","密码"
						,"邮箱","手机","性别","地址"});
		
		//无线连接账号信息
		String wlan_account_info = csvPath+"无限连接账号信息.csv";
		CsvWriter wlan_account_info_writer = createCsvWriter(wlan_account_info, 
				new String[] {"应用","账号","账号名","显示名","密码"
						,"邮箱","手机","性别","地址"});
		//支付宝账号信息
		String alipay_account_info = csvPath+"支付宝账号信息.csv";
		CsvWriter alipay_account_info_writer = createCsvWriter(alipay_account_info, 
				new String[] {"应用","账号","账号名","显示名","密码"
						,"邮箱","手机","性别","地址"});
		//微信好友统计
		String wx_friends = csvPath+"微信好友统计.csv";
		CsvWriter wx_friends_writer = createCsvWriter(wx_friends, 
				new String[] {"序号","ID","账号","昵称","真实姓名"
						,"手机","邮箱","数量"});
		
		//QQ好友统计
		String QQ_friends = csvPath+"QQ好友统计.csv";
		CsvWriter QQ_friends_writer = createCsvWriter(QQ_friends, 
				new String[] {"序号","ID","账号","昵称","真实姓名"
						,"手机","邮箱","数量"});
		//支付宝好友统计
		String alipay_friends = csvPath+"支付宝好友统计.csv";
		CsvWriter alipay_friends_writer = createCsvWriter(alipay_friends, 
				new String[] {"序号","账号","昵称","真实姓名"
						,"手机","邮箱","数量"});
		
		//微信群组统计
		String wx_groups = csvPath+"微信群组统计.csv";
		CsvWriter wx_groups_writer = createCsvWriter(wx_groups, 
				new String[] {"序号","账号","昵称","事件数量"});
		//QQ群组统计
		String QQ_groups = csvPath+"QQ群组统计.csv";
		CsvWriter QQ_groups_writer = createCsvWriter(QQ_groups, 
				new String[] {"序号","账号","昵称","事件数量"});
		//支付宝群组统计
		String alipay_groups = csvPath+"支付宝群组统计.csv";
		CsvWriter alipay_groups_writer = createCsvWriter(alipay_groups, 
				new String[] {"序号","账号","昵称","事件数量"});
		
		//微信群联系人
		String wx_group_contacts = csvPath+"微信群联系人.csv";
		CsvWriter wx_group_contacts_writer = createCsvWriter(wx_group_contacts, 
				new String[] {"序号","群账号","群昵称","群成员账号","群成员昵称"});
		
		//QQ群联系人
		String QQ_group_contacts = csvPath+"QQ群联系人.csv";
		CsvWriter QQ_group_contacts_writer = createCsvWriter(QQ_group_contacts, 
				new String[] {"序号","群账号","群昵称","群成员账号","群成员昵称"});
		
		//支付宝群联系人
		String alipay_group_contacts = csvPath+"支付宝群联系人.csv";
		CsvWriter alipay_group_contacts_writer = createCsvWriter(alipay_group_contacts, 
				new String[] {"序号","群账号","群昵称","群成员账号","群成员昵称"});
		
		//微信聊天记录
		String wx_chatrecord = csvPath+"微信聊天记录.csv";
		CsvWriter wx_chatrecord_writer = createCsvWriter(wx_chatrecord, 
				new String[] {"序号","时间","己方","动作","对方","内容"});
		
		//QQ聊天记录
		String QQ_chatrecord = csvPath+"QQ聊天记录.csv";
		CsvWriter QQ_chatrecord_writer = createCsvWriter(QQ_chatrecord, 
				new String[] {"序号","时间","己方","动作","对方","内容"});
		
		//支付宝聊天记录
		String alipay_chatrecord = csvPath+"支付宝聊天记录.csv";
		CsvWriter alipay_chatrecord_writer = createCsvWriter(alipay_chatrecord, 
				new String[] {"序号","时间","己方","动作","对方","内容"});
		
		//微信群聊记录
		String wx_group_chatrecord = csvPath+"微信群聊记录.csv";
		CsvWriter wx_group_chatrecord_writer = createCsvWriter(wx_group_chatrecord, 
				new String[] {"序号","时间","动作","对方","内容"});
		//QQ群聊记录
		String QQ_group_chatrecord = csvPath+"QQ群聊记录.csv";
		CsvWriter QQ_group_chatrecord_writer = createCsvWriter(QQ_group_chatrecord, 
				new String[] {"序号","时间","动作","对方","内容"});
		
		//支付宝群聊记录
		String alipay_group_chatrecord = csvPath+"支付宝群聊记录.csv";
		CsvWriter alipay_group_chatrecord_writer = createCsvWriter(alipay_group_chatrecord, 
				new String[] {"序号","时间","动作","对方","内容"});
		
		String tmp_account = "";
		String tmp_group = "";
		for(String html : htmls) {
			
			File input = new File(folder+html);
			
			Document document = Jsoup.parse(input,"UTF-8");
			Elements datatable = document.getElementsByClass("datatable");
			
			for(Element e : datatable) {
			
				//每个表的第一行都是字段，根据字段的内容判断表的内容
				Element firstTr = e.getElementsByTag("tr").first();
				Elements th = firstTr.getElementsByTag("th");
				
				//应用列表  判断标准：7个字段 且第二个字段为"应用"
				if(th.size()==7 && th.get(1).ownText().equals("应用")) {
					Elements trs = e.getElementsByTag("tr");
					
					for(int i = 1;i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText(),
								tds.get(6).ownText()};
						applist_writer.writeRecord(content);
					}
				}
				
				//通讯录，7个字段，且第7个字段为“归属地”
				if(th.size()==7 && th.get(6).ownText().equals("归属地")) {
					Elements trs = e.getElementsByTag("tr");
					/*System.out.println("-------");
					System.out.println(trs);*/
					
					for(int i = 1;i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText(),
								tds.get(6).ownText()};
						contacts_writer.writeRecord(content);
						
					}
				}
				
				//通话记录和短信，6个字段，且第五个字段为“号码”
				if(th.size()==6 && th.get(4).ownText().equals("号码")) {
					Elements trs = e.getElementsByTag("tr");
					/*System.out.println(trs);
					System.out.println("----------");*/
					
					for(int i = 1;i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText()};
						
						if(tds.get(2).ownText().contains("短信"))
							message_writer.writeRecord(content);//计入短信
						else
							calls_writer.writeRecord(content);//计入通话记录
						
						
					}
					
				}
				
				//账号信息,9个字段，第1个字段为“应用”的表为账号信息表
				if(th.size()==9 && th.get(0).ownText().equals("应用")) {
					Elements trs = e.getElementsByTag("tr");
					
					
					for(int i = 1; i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText(),
								tds.get(6).ownText(),tds.get(7).ownText(),
								tds.get(8).ownText()};
						
						if(tds.get(0).ownText().equals("微信")) {
							wx_account_info_writer.writeRecord(content);
						}else if(tds.get(0).ownText().equals("QQ")) {
							QQ_account_info_writer.writeRecord(content);
						}else if (tds.get(0).ownText().equals("无线连接")) {
							wlan_account_info_writer.writeRecord(content);
						}else if (tds.get(0).ownText().equals("支付宝")) {
							alipay_account_info_writer.writeRecord(content);
						}
					}
				}
				
				
			
				
				//好友的好友统计 ，8个字段，且第四个内容为"昵称 " 则为联系人table
				if(th.size()==8 && th.get(3).ownText().equals("昵称") ) {
					
					Elements trs = e.getElementsByTag("tr");
					
					//这里之所以从1开始，是因为每个table的第一行是描述字段
					for(int i = 1;i<trs.size();i++) {
						
						
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						boolean flag = true;
						Element e_account = e.previousElementSibling();
						for(int j=0;j<14;j++) {
							if(e_account!=null) {
								if(e_account.attr("class").equals("subtitle2"))
									break;
								e_account = e_account.previousElementSibling();
							}else {
								flag = false;
								break;
							}
							
						}
						if(flag) {
							tmp_account = e_account.ownText();
						}
						
						String[] content = {tmp_account,tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText(),
								tds.get(6).ownText(),tds.get(7).ownText()};
						
						
						/*
						 * 支付宝id  全是数字且16位
						 * 微信id 包含字母
						 * QQid 全是数字，位数低于16位
						 */
						if(isInteger(tds.get(1).ownText())) {
							if(tds.get(1).ownText().length()==16) {
								
								alipay_friends_writer.writeRecord(content);
							}
							else {
								QQ_friends_writer.writeRecord(content);
							}
						}else {
							wx_friends_writer.writeRecord(content);
						}
					
					}
				}
				
				//群组统计，4个字段，且第四个字段为“事件数量”
				if(th.size()==4 && th.get(3).ownText().equals("事件数量")) {
					Elements trs = e.getElementsByTag("tr");
					
					//这里之所以从1开始，是因为每个table的第一行市描述字段
					for(int i = 1; i<trs.size();i++) {
						
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						String[] content = {tmp_account,tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText()};
						
						/*
						 * 微信群号  5485113785@chatroom  末尾有@chatroom
						 * QQ群 纯数字
						 * 支付宝群   数字和字母
						 */
						if(tds.get(1).ownText().contains("@chatroom")) {
							wx_groups_writer.writeRecord(content);
						}else if (isInteger(tds.get(1).ownText())) {
							QQ_groups_writer.writeRecord(content);
						}else {
							alipay_groups_writer.writeRecord(content);
						}
					}
					
				}
				
				//群联系人，5个字段，且第4个字段为”群成员账号“
				if(th.size()==5 && th.get(3).ownText().equals("群成员账号")) {
					Elements trs = e.getElementsByTag("tr");
					
					for(int i = 1; i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),tds.get(4).ownText()};
						if(tds.get(1).ownText().contains("@chatroom")) {
							wx_group_contacts_writer.writeRecord(content);
						}else if (isInteger(tds.get(1).ownText())) {
							QQ_group_contacts_writer.writeRecord(content);
						}else {
							alipay_group_contacts_writer.writeRecord(content);
						}
						
					}
					
				}
				
				//和好友的聊天记录，5个字段，第5个字段为内容的为“聊天记录”
				if(th.size()==5 && th.get(4).ownText().equals("内容")) {
					Elements trs = e.getElementsByTag("tr");
					
					//这里之所以从1开始，是因为每个table的第一行市描述字段
					for(int i = 1; i<trs.size();i++) {
						
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						//如果序号前面的图片的src为“Images\delete.png”则此消息为已删除消息
						/*if(tds.get(0).getElementsByTag("img").attr("src").equals("Images\\delete.png")) {
							System.out.println(tds.get(4).ownText());
							System.out.println(folderName+html);
						}*/
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),e.previousElementSibling().ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),tds.get(4).ownText()};
						/*判断逻辑
						 * 1.QQ聊天记录：对方内容全是数字
						 * 2.微信群聊 ：对方包含"@chatroom"字段
						 * 3.支付宝只要动作为 转账的记录
						 * 4.QQ群聊：数字-数字 其他 "^(\\d+)(-)(\\d+).*"
						 * 5.微信聊天记录：以字母开头的字符串
						 * 6.支付宝群聊记录未知
						 */
						
						if(isInteger(tds.get(3).ownText()) || isInteger(tds.get(3).ownText().split(" ")[0])) {//特殊情况458874792 (四季如春(四季如春)) 
							QQ_chatrecord_writer.writeRecord(content);
						}else if(tds.get(3).ownText().contains("@chatroom")) {
							String[] content0 = {tds.get(0).ownText(),tds.get(1).ownText(),
									tds.get(2).ownText(),tds.get(3).ownText(),tds.get(4).ownText()};
							wx_group_chatrecord_writer.writeRecord(content0);
						}else if(tds.get(2).ownText().contains("转账")){
							alipay_chatrecord_writer.writeRecord(content);
						}else if(isQQGroup(tds.get(3).ownText())) {
							String[] content0 = {tds.get(0).ownText(),tds.get(1).ownText(),
									tds.get(2).ownText(),tds.get(3).ownText(),tds.get(4).ownText()};
							QQ_group_chatrecord_writer.writeRecord(content0);
						}else if(isWxId(tds.get(3).ownText())){
							
							wx_chatrecord_writer.writeRecord(content);
						}else {
							String[] content0 = {tds.get(0).ownText(),tds.get(1).ownText(),
									tds.get(2).ownText(),tds.get(3).ownText(),tds.get(4).ownText()};
							alipay_group_chatrecord_writer.writeRecord(content0);
						}
						
				
					}
				}
			}
			
		}
		//flush and close CsvWriter
		List<CsvWriter> list = new ArrayList<>();
		list.add(applist_writer);
		list.add(contacts_writer);
		list.add(calls_writer);
		list.add(wx_account_info_writer);
		list.add(wx_friends_writer);
		list.add(wx_groups_writer);
		list.add(wx_group_contacts_writer);
		list.add(wx_chatrecord_writer);
		list.add(wx_group_chatrecord_writer);
		list.add(QQ_account_info_writer);
		list.add(QQ_friends_writer);
		list.add(QQ_groups_writer);
		list.add(QQ_group_contacts_writer);
		list.add(QQ_chatrecord_writer);
		list.add(QQ_group_chatrecord_writer);
		list.add(alipay_account_info_writer);
		list.add(alipay_friends_writer);
		list.add(alipay_groups_writer);
		list.add(alipay_group_contacts_writer);
		list.add(alipay_chatrecord_writer);
		list.add(alipay_group_chatrecord_writer);
		list.add(wlan_account_info_writer);
		closeCsvWriter(list);
		
		
		//删除不需要的文件
		if(!get_applist) {
			deleteFile(applist);
		}
		if(!get_contacts) {
			deleteFile(contacts);
		}
		if(!get_callsRecord) {
			deleteFile(calls);
		}
		if(!get_wechat) {
			deleteFile(wx_account_info);
			deleteFile(wx_friends);
			deleteFile(wx_groups);
			deleteFile(wx_group_contacts);
			deleteFile(wx_chatrecord);
			deleteFile(wx_group_chatrecord);
		}
		if(!get_QQ) {
			
			deleteFile(QQ_account_info);
			deleteFile(QQ_friends);
			deleteFile(QQ_groups);
			deleteFile(QQ_group_contacts);
			deleteFile(QQ_chatrecord);
			deleteFile(QQ_group_chatrecord);
		}
		if(!get_alipay) {
			
			deleteFile(alipay_account_info);
			deleteFile(alipay_friends);
			deleteFile(alipay_groups);
			deleteFile(alipay_group_contacts);
			deleteFile(alipay_chatrecord);
			deleteFile(alipay_group_chatrecord);
		}
		if(!get_systemInfo) {
			deleteFile(wlan_account_info);
		}
		
	}
	
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		String folder = "D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content\\";
		String csvPath = "D:\\csvPath\\";
		try {
			File file = new File(csvPath);
			if(!file.exists())
				file.mkdirs();
			
			//getDataInCsv(folder,csvPath);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("共耗时（ms）："+(end-start));

	}

}
